// hyb_yl/patient/patient.js
var app = getApp()
var adds = {};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ["男", "女"],
    index: "",
    current: 0,
    date: "",
    date1: "",
    date2: "",
    relative: [],
    array1: [],
    array2: [""],
    index2: "",
    index1: "",
    imgArr: [],
    imgArr1: [],
    img_arr: [],
    data_arr: [],
    lianxiren: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var bgc = wx.getStorageSync('color')
    var lpid = options.lpid
    var uniacid = app.siteInfo.uniacid;
    var openid = wx.getStorageSync('openid')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    app.util.request({
      'url': 'entry/wxapp/url',
      'cachetime': '0',
      success: function(res) {
        console.log(res)
        that.setData({
          url: res.data
        })
      },
    })
    that.setData({
      backgroundColor: bgc,
      uniacid: uniacid,
      openid: openid,
      lpid: lpid
    })
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  bindPickerChange1: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index1: e.detail.value
    })
  },
  bindPickerChange2: function(e) {
    var that = this
    var array_fid = that.data.array_fid
    console.log(array_fid)
    var fid = array_fid[e.detail.value].fid
    console.log(fid)
    this.setData({
      index1: e.detail.value,
      fid: fid
    })
  },
  bindDateChange1: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date1: e.detail.value
    })
  },
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  bindDateChange2: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date2: e.detail.value
    })
  },
  //下一步
  nextClick: function() {
    this.setData({
      current: 1
    })
  },
  nextClick2: function() {
    this.setData({
      current: 2
    })
  },
  nextClick3: function() {
    this.setData({
      current: 3
    })
  },
  //上一步
  lastClick: function() {
    this.setData({
      current: 0
    })
  },
  lastClick2: function() {
    this.setData({
      current: 0
    })
  },
  lastClick3: function() {
    this.setData({
      current: 1
    })
  },
  lastClick4: function() {
    this.setData({
      current: 2
    })
  },
  //添加联系人
  add: function(e) {
    var that = this
    var relative = that.data.relative
    var index = relative.length
    var obj = {}
    obj.names1 = "names_" + index
    obj.types1 = "types_" + index
    obj.phones1 = "phones_" + index
    relative.push(obj)
    that.setData({
      relative: relative
    })
  },
  chooseImgArr: function() {
    var that = this;
    console.log(this.data.img_arr)
    if (this.data.img_arr.length < 4) {
      wx.chooseImage({
        count: 4,
        sizeType: ['original', 'compressed'],
        success: function(res) {
          console.log(res)
          if (res.tempFilePaths.length == 4) {
            that.setData({
              hide: true
            })
          }
          that.setData({
            img_arr: that.data.img_arr.concat(res.tempFilePaths)
          })
        }
      })
    } else {
      wx.showToast({
        title: '最多上传四张图片',
        icon: 'loading',
        duration: 3000
      });
    }
  },
  // chooseImgArr1: function () {
  //   var that = this
  //   wx.chooseImage({
  //     count: 9, // 默认9
  //     sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
  //     sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
  //     success: function (res) {
  //       var imgArr = that.data.imgArr1.concat(res.tempFilePaths)
  //       that.setData({
  //         imgArr1: imgArr
  //       })
  //     },
  //   })
  // },
  // chooseImgArr1: function() {
  //   var that = this;
  //   console.log(this.data.img_arr1)
  //   if (this.data.img_arr.length < 4) {
  //     wx.chooseImage({
  //       count: 4,
  //       sizeType: ['original', 'compressed'],
  //       success: function(res) {
  //         console.log(res)
  //         if (res.tempFilePaths.length == 4) {
  //           that.setData({
  //             hide: true
  //           })
  //         }
  //         that.setData({
  //           img_arr1: that.data.img_arr1.concat(res.tempFilePaths)
  //         })
  //       }
  //     })
  //   } else {
  //     wx.showToast({
  //       title: '最多上传四张图片',
  //       icon: 'loading',
  //       duration: 3000
  //     });
  //   }
  // },
  //删除
  del: function(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var imgArr = that.data.img_arr
    imgArr.splice(index, 1)
    that.setData({
      imgArr: imgArr
    })
  },
  // del1: function(e) {
  //   var that = this
  //   var index = e.currentTarget.dataset.index
  //   var imgArr = that.data.img_arr1
  //   imgArr.splice(index, 1)
  //   that.setData({
  //     imgArr1: imgArr
  //   })
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.getFwleix()
    this.getOne()
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  inputClick10: function(e) {
    var that = this
    var lianxiren = that.data.lianxiren
    var index = e.currentTarget.dataset.index
    console.log(typeof lianxiren[index])
    if (typeof lianxiren[index] == "object") {
      lianxiren[index]["names"] = e.detail.value
    } else {
      var obj = {}
      obj["names"] = e.detail.value
      lianxiren[index] = obj
    }
    that.setData({
      lianxiren: lianxiren
    })
  },
  inputClick11: function(e) {
    var that = this
    var lianxiren = that.data.lianxiren
    var index = e.currentTarget.dataset.index
    if (typeof lianxiren[index] == "object") {
      lianxiren[index]["types"] = e.detail.value
    } else {
      var obj = {}
      obj["types"] = e.detail.value
      lianxiren[index] = obj
    }
    that.setData({
      lianxiren: lianxiren
    })
  },
  inputClick12: function(e) {
    var that = this
    var lianxiren = that.data.lianxiren
    var index = e.currentTarget.dataset.index
    if (typeof lianxiren[index] == "object") {
      lianxiren[index]["phones"] = e.detail.value
    } else {
      var obj = {}
      obj["phones"] = e.detail.value
      lianxiren[index] = obj
    }
    that.setData({
      lianxiren: lianxiren
    })
  },
  subClick: function(e) {
    console.log(e);
    var that = this
    var form_id = e.detail.formId
    var openid = wx.getStorageSync('openid')
    var value = e.detail.value
    adds = e.detail.value;
    var detail = e.detail.target.dataset.index
    var data_arr = that.data.data_arr;
    var lianxiren = that.data.lianxiren
    var lianxi = []
    lianxi.push(lianxiren)
    // var obj = Object.assign(value, lianxi)
    value.lianxi = lianxiren
    console.log(value)
    if (detail == 1) {
      console.log(data_arr)
      this.upload();
      wx.showToast({
        title: '上传中，请稍后',
        icon: "loading"
      })
      setTimeout(function() {
        app.util.request({
          'url': 'entry/wxapp/Inseruser',
          data: {
            value: value,
            data_arr: data_arr
          },
          success: function(res) {
            app.util.request({
              'url': 'entry/wxapp/QQemail',
              data: {
                cyname: value.cyname,
                phone: value.uerPhone
              },
              success: function (res) {
                console.log(res)
              }
            })
          }
        })
      }, 3000)
    }
    app.util.request({
      'url': 'entry/wxapp/UserFormId',
      'data': {
        form_id: form_id,
        openid: openid
      },
      success: function(res) {
        //console.log(res)
      }
    })
  },
  upload: function() {
    var that = this
    var uniacid = that.data.uniacid
    var data_arr = that.data.data_arr;
    var str = /13[0123456789]{1}\d{8}|15[0123456789]\d{8}|17[0123456789]\d{8}|18[0123456789]\d{8}/;
    // if (str.test(adds.phone) == false) {
    //   wx.showToast({
    //     title: '手机号码不对',
    //     image: "/hyb_yl/images/err.png"
    //   })
    // } else {


    // }
    for (var i = 0; i < this.data.img_arr.length; i++) {
      wx.uploadFile({
        url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Upload&m=hyb_yl',
        filePath: that.data.img_arr[i],
        name: 'upfile',
        formData: adds,
        success: function(res) {
          console.log(res)
          data_arr.push(res.data)
          that.setData({
            data_arr: data_arr
          })
        }
      })
    }
    this.setData({
      formdata: ''
    })
  },
  getOne: function() {
    var that = this
    var lpid = that.data.lpid 
    app.util.request({
      'url': 'entry/wxapp/Alonelip',
      data: {
        lpid:lpid
      },
      success: function(res) {
        console.log(res)
        var array1 = that.data.array1
        console.log(array1)
        for (var i = 0; i < array1.length;i++){
          if (array1[i] == res.data.data.fwname){
            var index1 = i
          }
        }
        that.setData({
          alone:res.data.data,
          relative:res.data.data.name_0,
          lianxiren: res.data.data.name_0,
          img_arr: res.data.data.userpic,
          index:res.data.data.sex,
          date1: res.data.data.date1,
          date2: res.data.data.date2,
          date: res.data.data.date,
          index1: index1
        })
      }
    })
  },
  //服务类型
  getFwleix: function () {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/Fwleix',
      success: function (res) {
        console.log(res.data.data.fwname)
        that.setData({
          array1: res.data.data.fwname,
          array_fid:res.data.data
        })
      }
    })
  }
})